package br.project.cdd.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import br.project.cdd.bean.FaturamentoBean;

public class FaturamentoDAO extends Conexao {
	
	public void inserirFaturamento(FaturamentoBean bean){
		
		try{
			String sql = "INSERT INTO FATURAMENTO (ID, PARTICIPACAO, DESCONTO, FATURAMENTO, DATA_IMPORTACAO) VALUES(" + bean.getIdCdd() + ", '" + bean.getParticipacao() + "', '" + bean.getDesconto() + "', '" + bean.getFaturamento() + "', SYSDATE())";
			Statement stmt = conexao.createStatement();
			stmt.executeUpdate(sql);
		}
		catch(SQLException sql){
			sql.printStackTrace();
		}
	}
	
	public ArrayList<FaturamentoBean> selecionarFaturamento(){
		
		ArrayList<FaturamentoBean> listFatur = new ArrayList<FaturamentoBean>();
		FaturamentoBean fatur = null;
		
		try{
			
			String sql = "SELECT ID_FATURA, NOME_CDD, PARTICIPACAO, DESCONTO, FATURAMENTO FROM CDD C, FATURAMENTO F WHERE C.ID = F.ID";
			Statement stmt = conexao.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			
			while(rs.next()){
				fatur = new FaturamentoBean();
				fatur.setIdFatura(rs.getInt(1));
				fatur.setNomeCdd(rs.getString(2));
				fatur.setParticipacao(Double.parseDouble(rs.getString(3)));
				fatur.setDesconto(Double.parseDouble(rs.getString(4)));
				fatur.setFaturamento(Double.parseDouble(rs.getString(5)));
				
				listFatur.add(fatur);
			}
		}
		catch(SQLException sql){
			sql.printStackTrace();
		}
		
		return listFatur;
	}
}
